package weibo.sina;

import java.util.ArrayList;
import java.util.List;

import weibo.sina.impl.Status;
import weibo.sina.impl.Weibo;
import weibo.sina.impl.WeiboException;
import weibo.sina.impl.http.AccessToken;
import weibo.sina.impl.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.superc.R;
import com.superc.lib.helper.KeyValue;
import com.superc.lib.intent.IntentFunction;
import com.superc.lib.ui.UIFunction;
import com.superc.model.adapter.IAdapaterViewCallback;
import com.superc.model.adapter.ListItemAdapter;
import com.superc.model.notebook.NotebookItem;
import com.superc.notebook.NotebookItemView;

public class OAuthActivity extends Activity {
	
	private Activity my = null;
	private ListView lvwMessage=null;
	private ListItemAdapter<KeyValue<String,String>> adapter = null;
	private Weibo weibo=null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline);
		weibo=OAuthConstant.getInstance().getWeibo();
		weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
		my=this;
		Uri uri=this.getIntent().getData();
		try {
			RequestToken requestToken= OAuthConstant.getInstance().getRequestToken();
			AccessToken accessToken=requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
			OAuthConstant.getInstance().setAccessToken(accessToken);
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		
		initUI();
	}
	
	private void initUI() {
		UIFunction.getButton(my,R.id.cmdRefreshMessage).setOnClickListener(btnClick);
		UIFunction.getButton(my,R.id.cmdImportMessage).setOnClickListener(btnClick);
		adapter = new ListItemAdapter<KeyValue<String,String>>(this, R.layout.bloggermessageitem, getUserNews(),
				new IAdapaterViewCallback<KeyValue<String,String>>() {

					@Override
					public void OnCreateView(View parentView,
							KeyValue<String,String> item, int position,
							boolean bolFirstView) {
						TextView txtMessage = UIFunction.getTextView(
								parentView, R.id.txtBloggerMessage);
						txtMessage.setText(item.getKey() + ":" + item.getValue());
					}

					@Override
					public void OnViewClickListener(View v) {
						
					}
				});
		lvwMessage=UIFunction.getListView(this, R.id.lvwMessage);
		lvwMessage.setAdapter(adapter);
		adapter.addSelectedFeature(lvwMessage);
	}

	private List<KeyValue<String,String>> getUserNews(){
		List<Status> friendsTimeline;
		ArrayList<KeyValue<String,String>> results=new ArrayList<KeyValue<String,String>>(); 
		try {
			friendsTimeline = weibo.getFriendsTimeline();
			for (Status status : friendsTimeline) {
				results.add(new KeyValue<String,String>(status.getUser().getScreenName(),status.getText()));
			}
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return results;
	}
	
	private View.OnClickListener btnClick = new View.OnClickListener() {
		@SuppressWarnings("unchecked")
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.cmdRefreshMessage:{
					initUI();
					break;
				}
				case R.id.cmdImportMessage:{
					int index=adapter.getSelectedItemPosition();
					if(index!=-1){
						KeyValue<String,String> data=(KeyValue<String,String>) adapter.getItem(index);
						Intent notebookItemIntent=IntentFunction.getDirectIntent(my, NotebookItemView.class);
						NotebookItem item=new NotebookItem();
						item.setContent(data.getValue());
						notebookItemIntent.putExtra(NotebookItemView.BUNDEL_NAME, item);
						startActivityForResult(notebookItemIntent,0);
						break;
					}
					break;
				}
			}
		}
	};
	
}
